package com.example.pilipili.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pilipili.common.R;
import com.example.pilipili.dao.AdminPrivilegesMapper;
import com.example.pilipili.pojo.dto.APDto;
import com.example.pilipili.pojo.entity.AdminPrivileges;

import com.example.pilipili.pojo.entity.Privileges;
import com.example.pilipili.service.AdminPrivilegesService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liyv
 * @since 2023-12-01
 */
@Service
public class AdminPrivilegesServiceImpl extends ServiceImpl<AdminPrivilegesMapper, AdminPrivileges> implements AdminPrivilegesService {


    @Autowired
    AdminPrivilegesMapper apMapper ;

    @Override
    public R add(AdminPrivileges entry) {
        QueryWrapper<AdminPrivileges> qw = new QueryWrapper<>();
        qw.eq("aid",entry.getAid()).eq("pid",entry.getPid()) ;
        if(apMapper.exists(qw)) return R.failed("管理员已经具备该权限") ;
        // save
        apMapper.insert(entry);
        return R.ok(entry) ;
    }

    @Override
    public R del(AdminPrivileges entry) {
        QueryWrapper<AdminPrivileges> qw = new QueryWrapper<>();
        qw.eq("aid", entry.getAid()).eq("pid",entry.getPid()) ;
        apMapper.delete(qw) ;
        return R.ok();
    }

    @Override
    public R<List<APDto>> getUserPrivilegesList() {
        List<APDto> ups = apMapper.getUsers();
        for(APDto itm:ups){
            itm.setPrivileges(apMapper.getPListByUId(itm.getId()));
        }
        return R.ok(ups);
    }

    @Override
    public R<APDto> getPListByUId(Integer uid) {
        APDto dto = apMapper.getUserById(uid);
        dto.setPrivileges(apMapper.getPListByUId(uid));
        return R.ok(dto);
    }

    @Override
    public R<List<Privileges>> getByNotInAdmin(int aid) {
        return R.ok(apMapper.selectByNotInAdmin(aid));
    }
}
